<?php
require_once('start_session.php');

// header
require_once("header.php");
require_once('db_connect.php');
require_once('global_vars.php');
require_once('search_bar.php');
require_once('connect_info.php');
//require_once('survey_list.php');
if (isset($_COOKIE['user_id']) && $_COOKIE['user_id'] != ADMIN_ID)
	require_once('member_info.php');

//require_once("member_info.php");
// footer

//grant all privileges on * . * to famous@'%' identified by '1234'
?>

<div id="participate_main_div">
<?
//echo $_COOKIE['user_id'];
//echo $_SESSION['ip'];
//echo $_SESSION['connect_time'];

?>

<?php
$getv_sid = $_GET["sid"];

//date('Y-m-d', mktime(0, 0, 0, date('m'), $DD - $firstDate1, date('Y'))); 
//$a = date('Y-m-d H:i:s', mktime(14, 3, 2, 2, 3, 2016)); 
//echo $a;

	$sql = "select * from survey where id = $getv_sid";
	
	$result = mysqli_query($conn, $sql);
	$num_row = mysqli_num_rows($result);
	if($num_row != 1)
	{
		//die("sid 무효. 종료");
		echo "sid 무효. 종료<br />";
	}

	mysqli_query($conn, "update survey set view_count=view_count+1 where id = '$getv_sid'");
	$survey_row = mysqli_fetch_array($result);
	/*
	$survey_row[id]
	$survey_row[title];
	$survey_row[is_once];
	$survey_row[start_date];
	$survey_row[end_date];
	$survey_row[start_time];
	$survey_row[end_time];
	$survey_row[max_capacity];
	$survey_row[require_login];
	$survey_row[ap_id];
	$survey_row[gps_id];
	$survey_row[category];
	$survey_row[member_id];
	$survey_row[view_count];
	*/
	
	//타이틀,카테고리, 관련키워드, 기간, 시간대 출력
	?>
	<h3>설문 참여</h3>
	<hr noshade><br>
	<div id="participate_main_div_title">
	<font id="participate_main_font_head_title">설문 제목 : </font><font id="participate_main_font_head_contents_big"><?=$survey_row[title]?></font><br />
	<font id="participate_main_font_head_title">카테고리 : </font><font id="participate_main_font_head_contents_medium"><?=$survey_row[category]?></font><br />
	<font id="participate_main_font_head_title">관련 키워드 : </font><font id="participate_main_font_head_contents_medium">
	<?
	$result = mysqli_query($conn, "select * from keyword_survey where survey_id = '$survey_row[id]'");
	$keyword_survey_row = mysqli_fetch_array($result);
	echo "$keyword_survey_row[keyword]";
	while($keyword_survey_row = mysqli_fetch_array($result))
	{
		echo ",  $keyword_survey_row[keyword]";
	}
	?>
	</font><br />

	<font id="participate_main_font_head_title">설문 기간 : </font><font id="participate_main_font_head_contents_medium"><?=$survey_row[start_date]?> <font style='color:#0A6ECD;size:20px'>~</font> <?=$survey_row[end_date]?></font><br />
	<font id="participate_main_font_head_title">참여가능 시간대 : </font><font id="participate_main_font_head_contents_medium"><?=$survey_row[start_time]?> <font style='color:#0A6ECD;size:20px'>~</font> <?=$survey_row[end_time]?></font><br />
	</div>
	<?
	////기 본 설 문(현재 회원로그인 중일 경우 자동으로 채움)
	////[submit] 버튼 누르면 , [answer테이블]에 한 레코드가 저장됨
	//기본설문1. 성별
	?>
	<!--
	<iframe src="monitor.php"  frameborder="0" scrolling="no" marginwidth="0" marginheight="0"  width="147" height="100" id="monitor" onload="resizeFrame(this)" ></iframe>
	-->
	
	<form name=survey_main_form method=post>
	
	<input type=hidden name=answer_ip value='<?=$answer_ip?>'>
	<input type=hidden name=answer_connect_time value='<?=$answer_connect_time?>'>
	<input type=hidden name=answer_survey_id value='<?=$survey_row[id]?>'>
	<br />

<?
	$default_gender_1 = "";
	$default_gender_2 = "";
	$default_edulevel_1 = "";	
	$default_edulevel_2 = "";	
	$default_edulevel_3 = "";	
	$default_edulevel_4 = "";	
	$default_edulevel_5 = "";	
	$default_age_1 = "";
	$default_age_2 = "";
	$default_age_3 = "";
	$default_age_4 = "";
	$default_age_5 = "";
	$default_addr = "";

if(isset($_COOKIE['user_id']))
{
	$result = mysqli_query($conn, "select * from member where id = '".$_COOKIE['user_id']."'");
	$member_row = mysqli_fetch_array($result);

	if($member_row[gender] == 1)
	{
		$default_gender_1 = "checked";
	}
	else if($member_row[gender] == 2)
	{
		$default_gender_2 = "checked";
	}

	if($member_row[edulevel] == "1")
	{
		$default_edulevel_1 = "checked";
	}
	else if($member_row[edulevel] == "2")
	{
		$default_edulevel_2 = "checked";
	}
	else if($member_row[edulevel] == "3")
	{
		$default_edulevel_3 = "checked";
	}
	else if($member_row[edulevel] == "4")
	{
		$default_edulevel_4 = "checked";
	}
	else if($member_row[edulevel] == "5")
	{
		$default_edulevel_5 = "checked";
	}
	
	if($member_row[age] < 20)
	{
		$default_age_1 = "checked";
	}	
	else if($member_row[age] < 30)
	{
		$default_age_2 = "checked";
	}	
	else if($member_row[age] < 40)
	{
		$default_age_3 = "checked";
	}	
	else if($member_row[age] < 50)
	{
		$default_age_4 = "checked";
	}	
	else
	{
		$default_age_5 = "checked";
	}
	
	$default_addr = $member_row[addr];
}
?>	

<hr noshade>
<br />
<div id="participate_main_div_contents">
	<font id="participate_main_font_question_num">1.</font> <font id="participate_main_font_question">당신의 성별을 무엇입니까?</font><br />
	<div id="participate_main_div_contents_item">
		<input type=radio name=answer_gender value='1' <?=$default_gender_1?>>&nbsp;남&nbsp;&nbsp;
		<input type=radio name=answer_gender value='2' <?=$default_gender_2?>>&nbsp;여
	</div>
	<br />
	<font id="participate_main_font_question_num">2.</font> <font id="participate_main_font_question">당신의 최종학력은 무엇입니까?</font><br />
	<div id="participate_main_div_contents_item">
		<input type=radio name=answer_edulevel value='1' <?=$default_edulevel_1?>>&nbsp;중졸이하&nbsp;&nbsp;
		<input type=radio name=answer_edulevel value='2' <?=$default_edulevel_2?>>&nbsp;고졸&nbsp;&nbsp;
		<input type=radio name=answer_edulevel value='3' <?=$default_edulevel_3?>>&nbsp;초대졸&nbsp;&nbsp;
		<input type=radio name=answer_edulevel value='4' <?=$default_edulevel_4?>>&nbsp;대졸&nbsp;&nbsp;
		<input type=radio name=answer_edulevel value='5' <?=$default_edulevel_5?>>&nbsp;대학원졸
	</div>
		
	<br />
	<font id="participate_main_font_question_num">3.</font> <font id="participate_main_font_question">당신의 연령대는 어떻게 되십니까?</font><br />
	<div id="participate_main_div_contents_item">
		<input type=radio name=answer_age value='1' <?=$default_age_1?>>&nbsp;10대 이하&nbsp;&nbsp;
		<input type=radio name=answer_age value='2' <?=$default_age_2?>>&nbsp;20대&nbsp;&nbsp;
		<input type=radio name=answer_age value='3' <?=$default_age_3?>>&nbsp;30대&nbsp;&nbsp;
		<input type=radio name=answer_age value='4' <?=$default_age_4?>>&nbsp;40대&nbsp;&nbsp;
		<input type=radio name=answer_age value='5' <?=$default_age_5?>>&nbsp;50대 이상
	</div>
	
	<br />
	<font id="participate_main_font_question_num">4.</font> <font id="participate_main_font_question">당신의 거주지는 어디입니까?</font><br />
	<div id="participate_main_div_contents_item">
		<input type="text" size="35" name="answer_addr" value="<?=$default_addr?>">
		<input type="button" value="주소검색" onclick=openPopup("select_location.php?form_name=survey_main_form&addr_field_name=answer_addr","select",320,240)>
	</div>
	
	<br />
</div>
<hr noshade>
<br />
<div id="participate_main_div_contents">
	<?
	$result_question = mysqli_query($conn, "select * from question where survey_id = '$survey_row[id]'");

	$question_n = 4;										
	while($question_row = mysqli_fetch_array($result_question))
	{
		//question_row[survey_id]
		//question_row[qnumber]
		//question_row[qtitle]
		//question_row[is_multi]
		
		//한 문항씩 출력 - 	is_muiti가 참이면 항목들 : 체크박스
		//								아니면		   라디오
		
		$question_n++;
	?>
		<font id="participate_main_font_question_num2"><?=$question_n?>.</font> <font id="participate_main_font_question"><?=$question_row[qtitle]?></font><br />
		<div id="participate_main_div_contents_item">
	<?
		$result_item = mysqli_query($conn, "select * from item where survey_id = '$survey_row[id]' and qnum = '$question_row[qnumber]'"); 
		//echo("select * from item where survey_id = '$survey_row[id]' and qnum = '$question_row[qnumber]'");
		$item_n = 0;
	
		//case 1. 복수응답 불가 문항인 경우(라디오)
		if($question_row[is_multi] == 0)
		{
			while($item_row = mysqli_fetch_array($result_item))
			{
				//item_row[survey_id]
				//item_row[qnum]
				//item_row[inum]
				//item_row[ititle]
				$item_n++;
				echo "\t<input type=radio name='$question_row[qnumber]' value='$item_row[inum]'>&nbsp;$item_row[ititle]&nbsp;&nbsp;\n";
			}
		}
		//case 2. 복수응답 가능 문항인 경우(체크박스)
		else if($question_row[is_multi] == 1)
		{
			while($item_row = mysqli_fetch_array($result_item))
			{
				$item_n++;
				echo "\t<input type=checkbox name='$question_row[qnumber]_$item_row[inum]' value='1'>&nbsp;$item_row[ititle]&nbsp;&nbsp;\n";
			}
		}
		else
		{
			die("question_row[is_multi] 에러");
		}
		?>
		</div><br />
		<?
	}//모든 문항(question테이블에서 survey_id를 가진 모든 문항) 출력 끝
?>
<hr noshade><br>
<center><input type=button onclick=check_submit() value='작성완료'></center>

</form>

</div>








<iframe style="display:none" src="refresh.php"></iframe>
<iframe style="display:none" src="monitor.php"></iframe>
<div id="footer">
</div>
</div>
</body>
<script type="text/javascript">

function check_submit()
{

var e;
var sum;

//기본문항에 대한 에러처리
sum = 0;
e = document.getElementsByName("answer_gender");
sum = e[0].checked + e[1].checked;
if(sum == 0) { 
	alert('1번 문항에 응답하지 않았습니다.');
	document.getElementsByName("answer_gender")[0].focus();
	return;
}

sum = 0;
e = document.getElementsByName("answer_edulevel");
sum = e[0].checked + e[1].checked + e[2].checked + e[3].checked + e[4].checked;
if(sum == 0) { 
	alert('2번 문항에 응답하지 않았습니다.');
	document.getElementsByName("answer_edulevel")[0].focus();
	return;
}

sum = 0;
e = document.getElementsByName("answer_age");
sum = e[0].checked + e[1].checked + e[2].checked + e[3].checked + e[4].checked;
if(sum == 0) { 
	alert('3번 문항에 응답하지 않았습니다.');
	document.getElementsByName("answer_age")[0].focus();
	return;
}


e = document.survey_main_form.answer_addr.value;
if(e == '')
{
	alert('4번 문항에 응답하지 않았습니다.');
	document.survey_main_form.answer_addr.focus();
	return;
}

//일반문항에 대한 에러처리
<?
	$sql = "select * from survey where id = $getv_sid";
	
	$result = mysqli_query($conn, $sql);
	$survey_row = mysqli_fetch_array($result);
	
	$result_question = mysqli_query($conn, "select * from question where survey_id = '$survey_row[id]'");
	$question_n = 4;
	
	while($question_row = mysqli_fetch_array($result_question))
	{
		$question_n++;
	
		$result_item = mysqli_query($conn, "select * from item where survey_id = '$survey_row[id]' and qnum = '$question_row[qnumber]'"); 
		$item_n = 0;
		//case 1. 복수응답 불가 문항인 경우(라디오)
		if($question_row[is_multi] == 0)
		{
			echo "sum = 0;\n";
			echo "e = document.getElementsByName(\"$question_row[qnumber]\");\n";

			while($item_row = mysqli_fetch_array($result_item))
			{
				echo "sum += e[$item_n].checked;\n";
				$item_n++;
			}
			
			echo "if(sum == 0) { alert('".$question_n."번 문항에 응답하지 않았습니다.'); document.getElementsByName(\"$question_row[qnumber]\")[0].focus(); return; }\n";
			
		}
		//case 2. 복수응답 가능 문항인 경우(체크박스)
		else if($question_row[is_multi] == 1)
		{
			echo "sum = 0;\n";
			$a = -1;
			while($item_row = mysqli_fetch_array($result_item))
			{
				if($item_n == 0) $a = $item_row[inum];
				echo "e = document.getElementsByName(\"$question_row[qnumber]_$item_row[inum]\");\n";
				echo "sum += e[0].checked;\n";
				$item_n++;
				//echo "\t<input type=checkbox name='$question_row[qnumber]_$item_row[inum]' value='1'>&nbsp;$item_row[ititle]&nbsp;&nbsp;\n";
			}

			echo "if(sum == 0) { alert('".$question_n."번 문항에 응답하지 않았습니다.'); document.getElementsByName(\"$question_row[qnumber]_$a\")[0].focus(); return; }\n";
		}
		echo "\n";
	}//모든 문항(question테이블에서 survey_id를 가진 모든 문항) 출력 끝
?>
	//체크를 통과했다면 여기까지 옴.
	document.survey_main_form.action = "survey_participate_submit.php"  //여기에 서브밋 되는 주소를 넣으시면 되구요.
	document.survey_main_form.submit(); //체크후 폼 서브밋
}

</script> 
</html>

<?
mysqli_close($conn);
?>

